package cn.xyh.service.impl;

import cn.xyh.dao.RoleWriteMapper;
import cn.xyh.dao.UserWriteMapper;
import cn.xyh.exception.UserException;
import cn.xyh.model.Role;
import cn.xyh.model.User;
import cn.xyh.service.IUserService;
import org.apache.ibatis.transaction.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserWriteMapper userWriteMapper;
    @Autowired
    private RoleWriteMapper roleWriteMapper;


    /**
     * 新增
     */
    @Override
//    @Transactional(readOnly = false, rollbackForClassName = {"UserException", "RoleException"})
    public boolean insertUser(User entity) throws UserException {
        Integer result = 0;
        Integer res = 0;
        try {
            result = userWriteMapper.insertUser(entity);
        } catch (Exception e) {
            e.getMessage();
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new UserException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result >= 1 && res >= 1;
    }

    /**
     * 删除多个
     */
    @Override
    public Integer delUserByIds(String entityIds) throws UserException {
        Integer result = 0;
        try {
            String[] _entityIds = entityIds.split(",");
            result = userWriteMapper.delUserByIds(_entityIds);

        } catch (Exception e) {
            throw new UserException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 删除
     */
    @Override
    public Integer delUser(long id) throws UserException {
        Integer result = 0;
        try {

            result = userWriteMapper.delUser(id);

        } catch (Exception e) {
            throw new UserException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }


    /**
     * 修改
     */
    @Override
    public Integer updateUser(User entity) throws UserException {
        int result = 0;

        try {

            result = userWriteMapper.updateUser(entity);

        } catch (Exception e) {
            throw new UserException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 根据ID查找对象
     */
    @Override
    public User getUserById(long userId) throws UserException {

        User entity = null;
        try {
            entity = userWriteMapper.getUserById(userId);

        } catch (Exception e) {
            throw new UserException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return entity;
    }

    /**
     * 根据Code查找对象
     */
    @Override
    public User getUserByCode(String code) throws UserException {

        User entity = null;
        try {
            entity = userWriteMapper.getUserByCode(code);

        } catch (Exception e) {
            throw new UserException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return entity;
    }

    /**
     * 根据name查找对象
     */
    @Override
    public User getUserByName(String name) throws UserException {

        User entity = null;
        try {
            entity = userWriteMapper.getUserByName(name);
        } catch (Exception e) {
            e.getMessage();
            throw new UserException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return entity;
    }

    /**
     * 根据条件查找列表总数
     */
    @Override
    public Integer getUserCount(User entity) throws UserException {
        int result = 0;
        try {
            result = userWriteMapper.getUserCount(entity);

        } catch (Exception e) {
            throw new UserException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 根据条件查找列表
     *
     * @param entity      实体对象
     * @param startRownum 开始条数
     * @param endRownum   结束条数
     * @param sortField   排序字段
     * @param sortOrder   正序/倒序
     */
    @Override
    public List<User> getUserList(User entity) throws UserException {


        List<User> list = null;

        try {
            list = userWriteMapper.getUserList(entity);

        } catch (Exception e) {
            throw new UserException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return list;
    }
}

